package cn.qhuan.modules.service.impl;

import cn.qhuan.modules.constos.PageResponseResult;
import cn.qhuan.modules.constos.StatusConstos;
import cn.qhuan.modules.dtos.DynastyDto;
import cn.qhuan.modules.dtos.DynastyEnableDto;
import cn.qhuan.modules.entity.Dynasty;
import cn.qhuan.modules.mapper.DynastyMapper;
import cn.qhuan.modules.service.IDynastyService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description: 朝代表
 * @Author: 司翔宇
 * @Date:   2020-12-18
 * @Version: V1.0
 */
@Service
public class DynastyServiceImpl extends ServiceImpl<DynastyMapper, Dynasty> implements IDynastyService {

    @Autowired
    private DynastyMapper dynastyMapper;

    @Override
    public Result<?> updateEnableStatus(DynastyEnableDto dto, Integer type) {
        //1.检查参数
        if (dto == null || dto.getId() == null ){
            return Result.error("缺少参数");
        }
        //2.查询作品信息
        Dynasty dynasty= dynastyMapper.selectById(dto.getId());
        if (dynasty == null ){
            return Result.error("作品不存在");
        }
        //修改作品展示状态
        //不显示
        if (type.equals(StatusConstos.DIS_ABLE)){
            updateEnable(dynasty,StatusConstos.DIS_ABLE);
        }else if (type.equals(StatusConstos.EN_ABLE)){
            updateEnable(dynasty,StatusConstos.EN_ABLE);
        }
        return Result.OK("修改成功");
    }

    @Override
    public Result<?> queryByKeyWord(DynastyDto dto) {
        //1.检查参数
        if ( dto == null){
            return Result.error("缺少参数");
        }
        //分页检查
        dto.checkParam();
        //关键字模糊查询
        IPage pageParam =new Page(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<Dynasty> lambdaQueryWrapper =new LambdaQueryWrapper<>();

        if (StringUtils.isNotBlank(dto.getKeyword())){
            lambdaQueryWrapper.like(Dynasty::getName,dto.getKeyword());
        }
        //封装结果进行返回
        IPage pageList = page(pageParam, lambdaQueryWrapper);

        PageResponseResult responseResult =new PageResponseResult(dto.getPage(),dto.getSize(), (int) pageList.getTotal());
        responseResult.setResult(pageList.getRecords());
        return responseResult;
    }

    /**
     * 修改作品是否启用状态
     * @param dynasty
     * @param enableStatus
     */
    private void updateEnable(Dynasty dynasty,Integer enableStatus){
        dynasty.setEnable(enableStatus.toString());
        dynastyMapper.updateById(dynasty);
    }
}
